<?php
namespace  Admin\Controller;
use        Think\Controller;

class DatabaseController  extends  AdminController{
	
	public function index(){
			      // 搜索

        $keyword = I('keyword', '', 'string');

        $condition = array('like','%' . $keyword . '%');

        $map['name|id'] = array($condition,$condition,'_multi' => true );

 

        // 获取所有用户

        

        $data_list = D('Database')->page(! empty($_GET["p"]) ? $_GET["p"] : 1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();   

        $page = new \Common\Util\Page(D('Database')->where($map)->count(), C('ADMIN_PAGE_ROWS'));

        

        foreach ($data_list as $key => &$val) {
        	$customerid=$val['customer'];
        	
            $val['customer']=get_customer_info($val['customer'],"customer_name");
            $val['c_user']=get_user_info($val['c_user'],"username");
            $val['server']=get_fields("Server",$val['server'],"server_name");
             
             
             $val['customer']="<a href='".U('customer/xq',array('id'=>$customerid))."' style='text-decoration:none'>".$val['customer']."</a>";            
			 $val['server']="<a href='".U('Server/index')."' style='text-decoration:none'>". $val['server']."</a>";                        
//			 $val['master']="<a href='".U('Master/index')."' style='text-decoration:none'>". $val['master']."</a>";                        
//			 $val['domain']="<a href='".U('Domain/index')."' style='text-decoration:none'>". $val['domain']."</a>";                                   
//			 $val['database']="<a href='".U('Database/index')."' style='text-decoration:none'>". $val['database']."</a>";    
            
            
            
            $tt['tablename']='database_type';
			$tt['fieldvalue']=$val['type'];
			$database_type=D('Descriptions')->where($tt)->find();
			$val['type']=$database_type['title'];
        
        
            $tt1['tablename']='database_size';
			$tt1['fieldvalue']=$val['size'];
			$database_size=D('Descriptions')->where($tt1)->find();
			$val['size']=$database_size['title']; 
             
        }

     

        // 使用Builder快速建立列表页面。

        $builder = new \Common\Builder\ListBuilder();

        $builder->setMetaTitle('数据库列表')

            ->addTopButton('addnew') // 设置页面标题

            ->addTopButton('delete') // 添加新增按钮

            ->setSearch('请输入网站名称/id', U('index')) // 添加删除按钮



            ->addTableColumn('name', '数据库名称')

            ->addTableColumn('server', '服务器')

            ->addTableColumn('type', '类型')

            ->addTableColumn('size', '空间大小')

            ->addTableColumn('account', '账号')

            ->addTableColumn('s_time', '开通时间','time')

            ->addTableColumn('e_time', '过期时间','time')

			->addTableColumn('c_user', '创建人')

           ->addTableColumn('customer', '客户')			

           ->addTableColumn('c_time', '创建时间','time')

 	     	 

            ->addTableColumn('right_button', '操作', 'btn')

            ->setTableDataList($data_list)

            ->setTableDataPage($page->show())

            ->addRightButton('edit')

            ->display();
	}
	
	
	public function add(){
		if(IS_POST){

			    $_POST['s_time']=strtotime($_POST['s_time']);
			    $_POST['e_time']=strtotime($_POST['e_time']);
				$_POST['c_user']=is_login();
				$_POST['c_time']=time();

				$web=D('Database');

				$data=$web->create();				

				if($data){

					$id=$web->add();

					if($id){

						$this->success('新增成功',U('index'));

					}else{

						$this->error('新增失败');

					}

				}else{

					$this->error($web->getError());

				}

			}else{
				
				$type=D('Descriptions')->where('tablename="database_type"')->field('fieldvalue as id,title')->select();
               $size=D('Descriptions')->where('tablename="database_size"')->field('fieldvalue as id,title')->select();

				$bulider=new \Common\Builder\FormBuilder();

				$bulider->setMetaTitle('添加数据库')//设置页面标题

				        ->setPostUrl(U('add'))//表单提交地址

						->addFormItem('name','text','数据库名称','数据库名称')
						->addFormItem('type','select2','类型','类型',$type)

						->addFormItem('size','select2','空间大小','空间大小',$size)

						->addFormItem('account','textarea','账号','账号')

						->addFormItem('s_time','date','开通时间','开通时间')

						->addFormItem('e_time','date','过期时间','过期时间')

						->addFormItem('server','server_list','服务器 ','服务器 ID')

						

						->addFormItem('customer','user_list','客户','客户 ID')

					 												

					//	->addFormItem('supplier','text','创建人','创建人')

					//	->addFormItem('remark','textarea','创建时间','创建时间')

						 

						->display();

						 

			}
	}
	
	public function  edit($id){
		$info=M('Database')->find($id);

			if(IS_POST){

				$server=M('Database');

				$_POST['s_time']=strtotime($_POST['s_time']);

				$_POST['e_time']=strtotime($_POST['e_time']);

			 

				$data=$server->create();

				if($data){

					$res=$server->save();

					if($res){

						$this->success('更新成功',U('index'));

					}else{

						$this->error('更新失败');

					}

				}else{

					$this->error($server->getError());

				}

			}else{
				$type=D('Descriptions')->where('tablename="database_type"')->field('fieldvalue as id,title')->select();
               $size=D('Descriptions')->where('tablename="database_size"')->field('fieldvalue as id,title')->select();

				$bulider=new \Common\Builder\FormBuilder();

				$bulider->setMetaTitle('编辑数据库')

				        ->setPostUrl(U('edit'))

						->addFormItem('id','hidden','ID','ID')

				   ->addFormItem('name','text','数据库名称','数据库名称')
						->addFormItem('type','select2','类型','类型',$type)

						->addFormItem('size','select2','空间大小','空间大小',$size)

						->addFormItem('account','text','账号','账号')

						->addFormItem('s_time','date','开通时间','开通时间')

						->addFormItem('e_time','date','过期时间','过期时间')

						->addFormItem('server','server_list','服务器 ','服务器 ID')

						

						->addFormItem('customer','user_list','客户 ','客户 ID')

						->setFormData($info)

						->display();

			}

	}
	
	
	
}
?>